package com.bgs.eat.config;

import com.bgs.eat.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TokenCheck implements HandlerInterceptor {

//    @Autowired
//    LoginService loginService;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        //todo 请求方式校验
        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        String method = request.getRequestURI();
        //todo 获取token令牌
        String token = request.getHeader("token");
        //todo 判断令牌是否为Null
        if(!StringUtils.isEmpty(token) && !token.equals("null")){
//            Object token1 = redisUtil.get(token);
//            if(token1 != null){
//                redisUtil.set("token",token1,3600);
//            } else {
//                TokenError.error(response,"登录失效，请重新登录","401");
//                return false;
//            }
        } else {
            TokenError.error(response,"登录失效，请重新登录","401");
            return false;
        }
        return true;
    }
}
